package com.hxzy.controller.admin.login;

import com.hxzy.common.annotates.StpMobileCheckLogin;
import com.hxzy.common.constants.RedisConstants;
import com.hxzy.common.resp.R;
import com.hxzy.common.util.MarketThreadLocalUtil;
import com.hxzy.controller.admin.login.req.AdminLoginReq;
import com.hxzy.controller.admin.login.resp.AdminLoginResp;
import com.hxzy.service.AdminLoginService;
import com.hxzy.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.error.Mark;

import javax.validation.Valid;

/**
 * 后台登录管理
 */
@Log4j2
@Api(tags = "后台-登录Api")
@RestController
@RequestMapping(value = "/api")
public class AdminLoginController {

    @Qualifier(value = "adminLoginServiceImpl")
    @Autowired
    private AdminLoginService  adminLoginService;

    @Qualifier(value = "adminTokenService")
    @Autowired
    private TokenService tokenService;

    @ApiOperation(value = "登录")
    @PostMapping(value = "/login")
    public R<String>  login(@RequestBody @Valid AdminLoginReq  req){
        //登录
        AdminLoginResp adminLoginResp = this.adminLoginService.login(req);

        //拿AdminLoginResp 去换  令牌(HttpSession的sessionId)
        String token=this.tokenService.createToken(adminLoginResp);

        return R.okHasData(token);
    }


    /**
     * 获取当前登录用户信息
     * @return
     */
    @ApiOperation(value = "获取当有用户信息")
    @GetMapping(value = "/user/info")
    public R<AdminLoginResp> getUserInfo(){
        log.info("AdminLoginController-getUserInfo，当前线程ID是:"+ Thread.currentThread().getId());

        return R.okHasData(MarketThreadLocalUtil.ADMIN_THREAD_LOCAL.get());
    }

    @ApiOperation(value = "退出")
    @PostMapping(value = "/logout")
    public R logout(){
        this.tokenService.logout();
        return R.ok();
    }


}
